#include "delaywdiget.h"
#include <QPalette>
#include <QPushButton>
#include <QDebug>
#include <QEvent>
#include <QPainter>

DelayWdiget::DelayWdiget()
{

    width = 240;
    height = 36;
    delayLayout = new QHBoxLayout;
    close = new QLabel;
    delay1 = new QLabel;
    delay2 = new QLabel;
    delay3 = new QLabel;

    mainWin = new QWidget;
    mainWinLayout = new QHBoxLayout;

    delayLayout->setMargin(0);
    delayLayout->addStretch(0);
    delayLayout->addWidget(close);
    delayLayout->addStretch(0);
    delayLayout->addWidget(delay1);
    delayLayout->addStretch(0);
    delayLayout->addWidget(delay2);
    delayLayout->addStretch(0);
    delayLayout->addWidget(delay3);
    delayLayout->addStretch(0);
    close->setText(tr("close"));
    delay1->setText(tr("3s"));
    delay2->setText(tr("6s"));
    delay3->setText(tr("9s"));

//    mainWinLayout->addWidget(mainWin);
//    mainWin->setLayout(delayLayout);
//    mainWin->setStyleSheet("QWidget{border-radius:10px;background-color:black;}");

    this->setLayout(delayLayout);
    this->installEventFilter(this);
    close->installEventFilter(this);
    delay1->installEventFilter(this);
    delay2->installEventFilter(this);
    delay3->installEventFilter(this);

    QPalette pal;
    pal.setColor(QPalette::WindowText,Qt::white);
    delay1->setPalette(pal);
    delay2->setPalette(pal);
    delay3->setPalette(pal);
    close->setPalette(pal);
    this->setFixedSize(width,height);
    //毛玻璃
//    this->setProperty("useSystemStyleBlur",true);
//    this->setAttribute(Qt::WA_TranslucentBackground,true);

//    QPalette ground_palette(this->palette());
//    //设置背景黑色
//    ground_palette.setColor(QPalette::Background, QColor(2,2,2,0.4 * 255));
//    this->setAutoFillBackground(true);
//    this->setPalette(ground_palette);
}

/*
    delay_emit(int num)
    num = 0 : close; num = 1 : delay1; num = 2 : delay2; num = 3 : delay3
*/
bool DelayWdiget::eventFilter(QObject *watched, QEvent *event){
    QPalette bule_palette;
    QPalette white_palette;
    bule_palette.setColor(QPalette::WindowText,QColor("#3790FA"));
    white_palette.setColor(QPalette::WindowText,Qt::white);


    if(event->type() == QEvent::MouseButtonPress){
        if(watched == close){
            qDebug()<< "close";
            delay1->setPalette(white_palette);
            delay2->setPalette(white_palette);
            delay3->setPalette(white_palette);
            emit delay_emit(0);
        }
        else if(watched == delay1){
            qDebug() << "delay1";
            delay1->setPalette(bule_palette);
            delay2->setPalette(white_palette);
            delay3->setPalette(white_palette);
            emit delay_emit(1);
        }
        else if(watched == delay2){
            qDebug() << "delay2";
            delay1->setPalette(white_palette);
            delay2->setPalette(bule_palette);
            delay3->setPalette(white_palette);
            emit delay_emit(2);
        }
        else if(watched == delay3){
            qDebug() << "dealy3";
            delay1->setPalette(white_palette);
            delay2->setPalette(white_palette);
            delay3->setPalette(bule_palette);
            emit delay_emit(3);
        }
    }

    return QWidget::eventFilter(watched,event);
}


void DelayWdiget::mousePressEvent(QMouseEvent *event){
    Q_UNUSED(event);
//    QPushButton *pButton = qobject_cast<QPushButton *>(sender());

//    QWidget::mousePressEvent(event);

}

void DelayWdiget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    painter.setBrush(QBrush((QColor(0,0,0,70))));
    painter.setPen(Qt::transparent);
    QRect rect = this->rect();
    rect.setWidth(rect.width() - 1);
    rect.setHeight(rect.height() - 1);
    painter.drawRoundedRect(rect, 10, 10);
    //也可用QPainterPath 绘制代替 painter.drawRoundedRect(rect, 15, 15);
//    {
//        QPainterPath painterPath;
//        painterPath.addRoundedRect(rect, 15, 15);
//        p.drawPath(painterPath);
//    }
    QWidget::paintEvent(event);
}


